<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px"
    >
      <el-form-item label="应用信息" prop="app_id">
        <el-select
          v-model="queryParams.app_id"
          placeholder="请选择应用信息"
        >
          <el-option
            v-for="item in appOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精确用户名称" prop="exact_name">
        <el-input
          v-model="queryParams.exact_name"
          placeholder="请输入精确用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker
          v-model="registerTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" icon="el-icon-search" size="mini"
          v-hasPermi="['customer:user:query']" @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @sort-change="sortChange">
      <el-table-column sortable="custom" label="用户编号" align="center" key="id" prop="id" v-if="showColumns.id"/>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        v-if="showColumns.operator"
      >
        <template slot-scope="scope" v-if="scope.row.id !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExchange(scope.row)"
            v-hasPermi="['customer:account:exchange']"
          >转换账户
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="会员名称" align="center" key="name" prop="name"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.name"
      />
      <el-table-column label="会员手机号" align="center" key="phone" prop="phone"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.phone"
      />
      <el-table-column label="一级代理" align="center" key="first_agent_name" prop="first_agent_name"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.first_agent_name"
      />
      <el-table-column label="注册时间" align="center" key="register_time" prop="register_time" width="160"
                       v-if="showColumns.register_time"/>
      <el-table-column sortable="custom" label="活期金额" align="center" key="amount" prop="amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.amount"
      />
      <el-table-column sortable="custom" label="投资金额" align="center" key="invest_amount" prop="invest_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.invest_amount"
      />
      <el-table-column sortable="custom" label="冻结金额" align="center" key="frozen_amount" prop="frozen_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.frozen_amount"
      />
      <el-table-column sortable="custom" label="智能金额" align="center" key="ai_amount" prop="ai_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.ai_amount"
      />
      <el-table-column sortable="custom" label="冻结智能金额" align="center" key="ai_frozen_amount"
                       prop="ai_frozen_amount" :show-overflow-tooltip="true" width="160"
                       v-if="showColumns.ai_frozen_amount"
      />
      <el-table-column sortable="custom" label="虚拟智能金额" align="center" key="virtual_ai_amount"
                       prop="virtual_ai_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.virtual_ai_amount"
      />
      <el-table-column sortable="custom" label="抽奖次数" align="center" key="lucky_draw_count"
                       prop="lucky_draw_count"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.lucky_draw_count"
      />
      <el-table-column label="累计充值" align="center" key="total_deposit" prop="total_deposit"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_deposit"
      />
      <el-table-column label="累计充值笔数" align="center" key="total_deposit_number" prop="total_deposit_number"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_deposit_number"
      />
      <el-table-column label="累计提现" align="center" key="total_withdraw" prop="total_withdraw"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_withdraw"
      />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <el-dialog :title="exchangeTitle" :visible.sync="exchangeOpen" width="800px" append-to-body
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio
                v-for="dict in dict.type.customer_account_exchange"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="金额" prop="amount">
            <el-input type="number" v-model="form.amount" maxlength="30"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exchange">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {getGroupApp} from "@/api/metadata/app";
import {exchange, listUser} from "@/api/customer/customerAccount";

export default {
  name: 'CustomerAccount',
  dicts: ['customer_account_exchange'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      exchangeTitle: '',
      // 是否显示弹出层
      exchangeOpen: false,
      admin: false,
      appOptions: [],
      // 日期范围
      registerTimeRange: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        app_id: undefined,
        name: undefined,
        exact_name: undefined,
        phone: undefined,
        sort_list: [],
      },
      // 表单校验
      rules: {
        amount: [
          {required: true, message: '金额不能为空', trigger: 'blur'},
        ],
        type: [
          {required: true, message: '类型不能为空', trigger: 'blur'},
        ]
      },
      columns: [
        {key: 1, name: 'id', label: `用户编号`, visible: true},
        {key: 2, name: 'operator', label: `操作`, visible: true},
        {key: 3, name: 'name', label: `用户名称`, visible: true},
        {key: 4, name: 'phone', label: `会员手机号`, visible: true},
        {key: 5, name: 'first_agent_name', label: `一级代理`, visible: true},
        {key: 6, name: 'register_time', label: `注册时间`, visible: true},
        {key: 7, name: 'amount', label: `活期金额`, visible: true},
        {key: 8, name: 'invest_amount', label: `投资金额`, visible: true},
        {key: 9, name: 'frozen_amount', label: `冻结金额`, visible: true},
        {key: 10, name: 'ai_amount', label: `智能金额`, visible: true},
        {key: 11, name: 'ai_frozen_amount', label: `冻结智能金额`, visible: true},
        {key: 12, name: 'virtual_ai_amount', label: `虚拟智能金额`, visible: true},
        {key: 13, name: 'lucky_draw_count', label: `抽奖次数`, visible: true},
        {key: 14, name: 'total_deposit', label: `累计充值`, visible: true},
        {key: 15, name: 'total_deposit_number', label: `累计充值笔数`, visible: true},
        {key: 16, name: 'total_withdraw', label: `累计提现`, visible: true},
      ],
    }
  },
  computed: {
    showColumns: function () {
      window.localStorage.setItem('CustomerAccountColumns-5', JSON.stringify(this.columns));
      let newShowColumns = {}
      for (let i = 0; i < this.columns.length; i++) {
        let item = this.columns[i];
        Object.defineProperty(newShowColumns, item.name, {value: item.visible});
      }
      return newShowColumns;
    }
  },
  created() {
    // 默认id排序
    this.queryParams.sort_list = [{sort_by: 'id', sort_type: 2}]
    this.getGroupApp()
    if (window.localStorage.getItem('CustomerAccountColumns-5')) {
      this.columns = JSON.parse(window.localStorage.getItem('CustomerAccountColumns-5'))
    }
  },
  methods: {
    sortChange(column) {
      if (column) {
        if (column.order === 'ascending') {
          this.queryParams.sort_list = [{sort_by: column.prop, sort_type: 1}]
        } else if (column.order === 'descending') {
          this.queryParams.sort_list = [{sort_by: column.prop, sort_type: 2}]
        }
        this.getList()
      }
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listUser(this.addDateRange(this.queryParams, this.registerTimeRange, '_register_time')).then(response => {
          this.userList = response.data
          this.total = response.total
          this.loading = false
        }
      )
    },
    getGroupApp() {
      getGroupApp().then(response => {
        this.queryParams.type = 1
        this.appOptions = response.data
        this.queryParams.app_id = response.data[0].id
        this.getList()
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 取消按钮
    cancel() {
      this.exchangeOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        amount: 0.00,
        customer_id: undefined,
        type: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.registerTimeRange = []
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.handleQuery()
    },
    /** 转换账户 */
    handleExchange: function (row) {
      this.reset()
      this.form.customer_id = row.id
      this.form.type = 15
      this.exchangeOpen = true
      this.exchangeTitle = '转换账户'
    },
    /** 提交按钮 */
    exchange: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          exchange(this.form).then(response => {
            this.$modal.msgSuccess('转换金额成功')
            this.exchangeOpen = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
